<?php
namespace Qwadmin\Controller;

class ProfileController extends ComController{
    /**
     * 收款信息
     */
    public function payment(){
		$member = D("member")->findUser(['uid' => session('uid')]);
		$this->assign("member" , $member);
        $this->display();
    }
    
    /**
     *  修改密码
     */
    public function password(){
        $prefix = C("DB_PREFIX");
        $old_password = htmlspecialchars(I("old_password" , ''));
		$new_password = htmlspecialchars(I("new_password" , ''));
		$ajax = I("ajax" , 0 , 'intval');
		
		if(!empty($ajax)){
			if(empty($old_password)){
				die(json_encode(['status'=>0,'msg'=>'旧密码不能为空']));
			}
			if(empty($new_password)){
				die(json_encode(['status'=>0,'msg'=>'新密码不能为空']));
			}
			if(!preg_match("/^[a-zA-Z0-9]{6,16}$/", $new_password)){
				die(json_encode(['status'=>0,'msg'=>'新密码格式错误']));
			}
			$member = D("member")->findUser(['uid' => session('uid')]);
			$password = password($old_password, $member['code']);
			if($member['password']!=$password){
				die(json_encode(['status'=>0,'msg'=>'旧密码不正确']));
			}
			$password = password($new_password, $member['code']);
			D("member")->editUser(['password'=>$password] , session('uid'));
			die(json_encode(['status'=>1,'msg'=>'密码修改成功']));
		}
        
        $this->display();
    }
    
    /**
     *  个人资料
     */
    public function index(){
		$prefix = C("DB_PREFIX");
        $member = M("member")->field("{$prefix}member.*,{$prefix}auth_group.title")
                ->join("{$prefix}auth_group ON {$prefix}auth_group.id = {$prefix}member.level")
                ->where(['uid'=>session('uid')])
                ->find();
		$this->assign("member" , $member);
        $this->display();
    }
    
    
}
